package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.common.annotations.Log;
import com.atguigu.spzx.manager.helper.MenuHelper;
import com.atguigu.spzx.manager.mapper.SysMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.model.pojo.system.SysMenu;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    /*
     * @description: 查找所有菜单，并返回树化之后的结果
     **/
    @Override
    public List<SysMenu> findMenuTree() {
        List<SysMenu> sysMenus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>().orderByAsc("sort_value"));
        sysMenus = MenuHelper.menuToTree(sysMenus);
        return sysMenus;
    }

    /*
     * @description: 删除菜单
     **/
    @Override
    public boolean doDeleteMenu(Long id) {
        DYLAssert.isNull(sysMenuMapper.selectOne(new QueryWrapper<SysMenu>().eq("parent_id", id)), "删除失败，该菜单中存在子节点！");
        sysMenuMapper.deleteById(id);
        return true;
    }

    /*
     * @description: 根据用户id获取用户菜单
     **/
    @Override
    public List<SysMenuVo> getMenuVoByUserId(Long id) {
        List<SysMenu> sysMenus = sysMenuMapper.findMenuByUserId(id);
        return MenuHelper.menuToVoTree(sysMenus);
    }
}
